package netty.pojo;

import com.alibaba.fastjson.JSON;
import com.lagou.entity.RpcRequest;
import com.lagou.serilize.JSONSerializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Description: 简化Decoder的写法
 * @Author: zhongyuan
 * @CreateDate: 2020/8/25
 * @Version: OSINT1.1
 */
public class TimeDecoder2 extends ReplayingDecoder<Void> {
    @Override
    protected void decode(
            ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        if (in.readableBytes() < 4 || in.readableBytes()<4+in.readInt()) {
            return;
        }
        int objLeng = in.readInt();
        JSONSerializer serializer = new JSONSerializer();
        byte[] objByteArr = new byte[objLeng];
        in.readBytes(objByteArr);
        RpcRequest request = serializer.deserialize(RpcRequest.class,objByteArr);
        out.add(request);
    }
}